/**
* Created by yyr on 2018/3/14
*已投资基金》添加
*/
<template>
    <div>
        <div class="right">
            <el-row>
                <el-col :span="24" class="ca-align-right">
                    <div class="ca-head-fixed">
                        <el-button type="primary" @click="fundAdd()">保存</el-button>
                        <el-button  @click="exit()">退出</el-button>
                    </div>
                </el-col>
            </el-row>
            <!--头部-->
            <csFundHeadEdit
                :isAdd="isAdd"
                ref="detailEdit"
                :fund="investedData.fund"
                :fundUserList="investedData.fundUserList"
                :industryList="investedData.industryList"
                @changeFundUser="changeFundUser"
                v-if="showChildren"
                @changeUserTeam="changeUserTeam">
            </csFundHeadEdit>
            <!--详情-->
            <div class="ca-m-t-20">
                <csFundBasicEdit
                    :isAdd="isAdd"
                    :fundEst="investedData.fundEst"
                    :committeeList="investedData.committeeList"
                    @changeCommitteeName="changeCommitteeName"
                    @closeCommitteeItem="closeCommitteeItem"
                    @changeRegion="changeRegion"
                >
                </csFundBasicEdit>
            </div>
            <!--管理机构-->
            <div class="ca-m-t-20 ca-box">
                <csFundManageOrgan
                    :isAdd="isAdd"
                    v-if="showChildren"
                    :fundMechanism="investedData.fundMechanism"
                    @changeRegion="changeRegionOrg"
                >
                </csFundManageOrgan>
            </div>
            <!--管理层-->
            <csFundManage
                :isAdd="isAdd"
                v-if="showChildren"
                @fundStratumList="getFundStratumList"
            ></csFundManage>
            <!--账户信息-->
            <csFundAccountEdit
                :isAdd="isAdd"
                :accountList="investedData.accountList"
            ></csFundAccountEdit>
        </div>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from './api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import { addRemind } from '__service/utils';
    //    import { isNoAuth } from '__service/utils';
    export default {
        data() {
            var validateNumber = (rule, value, callback) => {
                var reg = /^\d{1,14}(\.\d{1,6})?$/;
                if ((value !== '') && (!reg.test(value))) {
                    callback(new Error('请输入正确范围的数字'));
                } else {
                    callback();
                }
            };
            return {
                redId: '',
                showChildren: null,//控制头部组件的显示
//                showChildrenOrg: null,//控制机构显示
                investedData: {
                    accountList: [
                        {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 1},//基本户开户行
                        {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 2},//托管户开户行
                        {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 3},//监管户开户行
                        {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 4},//基金证券账户信息
                    ],
                    committeeList: [],
                    //头部信息
                    fund: {
                        establishTime: '',//成立时间
                        exitPeriod: '',//退出期
                        exitTime: '',//退出时间
                        fundCategory: '',//基金类别  fundCategory
                        fundFullname: '',//全称
                        fundName: '',//简称
                        fundScale: 0,//基金规模
                        fundState: 1,//设立状态0：拟设立，1：已设立，2：退出中,3：已到期,4:母基金 对应字典表（d_type=fundState） ,
                        fundType: '',// 基金类型:对应字典表（d_type=fundType） ,
                        id: '',
                        investmentPeriod: '',//投资期
                        treeType: 1,// 1：子基金；2：母基金 对应字典表：fundTree
                    },
                    //投前信息
                    fundAnsatEstList: [
//                        {
//                            ansatId: '',//拟设立基金
//                            estId: '',//已设立基金
//                            id: ''
//                        }
                    ],
                    //基本信息
                    fundEst: {
                        backups: '', //是否备份
                        company: '', //管理公司
                        creditCode: '', //统一信用代码
                        custodian: '', //基金托管人
                        disclosure: '', //信息披露
                        distribution: '', //收益分配
                        exitMode: '',//退出方式
                        fundDelegate: '', //基金委派代表
                        fundForm: '', //管理形式 字典fundForm
                        fundId: '', //
                        id: '',
                        icc: '',//是否在中基协备案
                        iccNumber: '',//中基协备案号
                        finance: '',//金融办备案
                        financeNumber: '',//金融办备案号
                        manType: '', //管理类型fundManType
                        officeLand: '', //办公地
                        other: '', //其他
                        partnerId: '', //执行事务合伙人id
                        partnerName: '', //执行事务合伙人名称
                        place: '', //注册地
                        proportion: '', //管理费比例
                        restrictions: '', //投资限制
                        investmentDirection: '',//投资方向
                        regionList: [
                            {
                                businessId: '',
                                businessType: 1,
                                city: '',
                                district: '',
                                id: '',
                                province: '',
                                registrationDetailed: '',
                            }
                        ]
                    },
                    //机构信息
                    fundMechanism: {
                        foundingTime: '',  //成立时间
                        fundId: '',
                        govern: '', //公司治理
                        id: '',
                        mechanism: '', //内控机制
                        mechanismName: '', //机构名称
                        partner: '', //执行事务合伙人
                        proportion: '', //股东出资金额和比例
                        receipts: '', //实收资本
                        region: [
                            {
                                businessId: '',//业务id
                                businessType: 1, //业务类型 1，注册地 2 办公地
                                city: '', //市
                                district: '', //区
                                id: '',
                                province: '', //省
                                registrationDetailed: '', //详细地址
                            }
                        ], //注册地
                        register: '', //注册资本
                        representative: '', //法定代表人
                        rule: '', //管理制度
                        subscribed: '', //认缴金额
                    },
                    //管理层
                    fundStratumList: [
                        {
                            fax: '', //传真
                            fundId: '', //
                            id: '',
                            mailbox: '', //邮箱
                            position: '', //职位
                            remarks: '', //备注
                            sex: '', //性别
                            tel: '', //电话
                            userName: '', //高管姓名
                        }
                    ],
                    fundUserList: [],
                    industryList: [],
                },
                fundEst: {
                    backups: '', //是否备份
                    company: '', //管理公司
                    creditCode: '', //统一信用代码
                    custodian: '', //基金托管人
                    disclosure: '', //信息披露
                    distribution: '', //收益分配
                    exitMode: '',//退出方式
                    fundDelegate: '', //基金委派代表
                    fundForm: '', //管理形式 字典fundForm
                    fundId: '', //
                    id: '',
                    icc: '',//是否在中基协备案
                    iccNumber: '',//中基协备案号
                    finance: '',//金融办备案
                    financeNumber: '',//金融办备案号
                    manType: '', //管理类型fundManType
                    officeLand: '', //办公地
                    other: '', //其他
                    partnerId: '', //执行事务合伙人id
                    partnerName: '', //执行事务合伙人名称
                    place: '', //注册地
                    proportion: '', //管理费比例
                    restrictions: '', //投资限制
                    investmentDirection: '',//投资方向
                    regionList: [
                        {
                            businessId: '',
                            businessType: 1,
                            city: '',
                            district: '',
                            id: '',
                            province: '',
                            registrationDetailed: '',
                        }
                    ]
                },
                initView: {
                    userEntityList: this.$store.state.user.department.userEntityList,//用户
                },
                rules: {
                    fundFullname: [
                        { required: true, message: '请输入基金全称', trigger: 'blur' },
                    ],
                    fundName: [
                        { required: true, message: '请输入基金简称', trigger: 'blur' },
                    ],
                    investmentPeriod: [
                        { required: true, message: '请输入投资期', trigger: 'blur' },
                    ],
                    exitPeriod: [
                        { required: true, message: '请输入退出期', trigger: 'blur' },
                    ],
                    fundScale: [
                        { required: false, validator: validateNumber, trigger: 'blur'}
                    ],
                },
            };
        },
        created() {
            this.isAdd = this.$route.params.itemId === '-1' ? true : false;
            this.init();
        },
        mounted() {
        },
        methods: {
            init() {
                if (this.$route.params.establishId ) {
                    apiHttp(api.ESTABLISH_CHANGE_INVESTING,{fundId: this.$route.params.establishId}).then(ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.investedData = ret.data;
                            this.showChildren = true;
                            this.investedData.fundEst = Object.assign({},this.fundEst)
                            this.investedData.accountList = [
                                {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 1},//基本户开户行
                                {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 2},//托管户开户行
                                {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 3},//监管户开户行
                                {accountNumber: '',fundId: '',id: '',name: '',nameOfBank: '',type: 4},//基金证券账户信息
                            ]
                            this.investedData.fundStratumList = [];
                            this.investedData.committeeList = [];
                            this.investedData.fund.fundState = '1';
                        }
                    })
                } else {
                    this.showChildren = true;
                }
            },
            exit() {
                /*退出新增页面*/
                this.$confirm('是否退出', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(() => {
                    this.$router.push({name: 'invested-funds-list'});
                })
            },
            getFundStratumList(fundStratumList) {
                this.investedData.fundStratumList = fundStratumList;
            },
            //添加
            async fundAdd() {
                let isSave = this.$refs.detailEdit.validateAdd();  // 验证通过，可以保存
                if (isSave) {
                    let {resCode,data} = await apiHttp(api.ADD_FUND, this.investedData);
                    if (resCode === STATUS_HTTP_SUCCESS) {
                        this.redId = data.id;
                        this.$message({
                            type: 'success',
                            message: '添加成功！',
                            center: true
                        })
                        this.addRedDot()
                        this.$router.push({name: 'invested-funds-list', params: {openWindow: false}})
                    }
                }
            },
            // 改变注册地点
            changeRegion(val) {
                this.investedData.fundEst.regionList = [];
                this.investedData.fundEst.regionList.push({
                    businessId: '',
                    businessType: val.businessType,
                    city: val.city,               // 市
                    district: val.district,       // 区
                    id: '',                       // id
                    province: val.province,       // 省
                    registrationDetailed: val.registrationDetailed,  // 详细地址
                })
            },
            // 添加小红点
            async addRedDot() {
                await addRemind({
                    dataId: this.redId, // 业务id
                    dataTab: 0, // tab页顺序 head 也为0 新建为'-1'
                    dataType: 5, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                });
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
                // 添加小红点 end
            },
            // 改变注册地点
            changeRegionOrg(val) {
                this.investedData.fundMechanism.region = [];
                this.investedData.fundMechanism.region.push({
                    businessId: '',
                    businessType: val.businessType,
                    city: val.city,               // 市
                    district: val.district,       // 区
                    id: '',                       // id
                    province: val.province,       // 省
                    registrationDetailed: val.registrationDetailed,  // 详细地址
                })
            },
            //改变基金经理
            changeFundUser(val) {
                this.investedData.fundUserList.map((userItem, index)=>{//如果基金经理存在，就清空
                    if (userItem.userType === '1') {
                        this.investedData.fundUserList.splice(index,1)
                    }
                })
                this.initView.userEntityList.map(user=>{//更改基金经理
                    if (user.id === val) {
                        this.investedData.fundUserList.push({
                            fundId: '', //基金主id
                            id: '',    //主id
                            userId: val,  //用户id
                            userName: user.userName,  //用户名称
                            userType: '1',  //用户类型  1：基金经理；2：基金管理团队
                        })
                    }
                })
            },
            //添加管理团队
            changeUserTeam(teamId) {
                let noRepeat = [];
                if (this.investedData.fundUserList.length > 0 ) {//数组中已有数据，判断是否重复
                    this.investedData.fundUserList.map(user => {
                        if (user.userType === '2') {
                            noRepeat.push(user.userId);
                        }
                    })
                }
                if (noRepeat.indexOf(teamId) === -1) {//没有重复，添加
                    this.initView.userEntityList.map(user=>{//更改基金经理
                        if (user.id === teamId) {
                            this.investedData.fundUserList.push({
                                fundId: '', //基金主id
                                id: '',    //主id
                                userId: teamId,  //用户id
                                userName: user.userName,  //用户名称
                                userType: '2',  //用户类型  1：基金经理；2：基金管理团队
                            })
                        }
                    })
                }
            },
            //添加投资决策委员会
            changeCommitteeName(val) {
                let flag = false;
                for (let i of this.investedData.committeeList) {
                    if (i.committeeId === val) {
                        flag = true;
                        return;
                    }
                }
                if (!flag) {
                    for (let i of this.initView.userEntityList) {
                        if (val === i.id) {
                            this.investedData.committeeList.push({committeeId: i.id,committeeName: i.userName,fundId: '',id: ''})
                        }
                    }
                }
            },
            //删除一个投资决策
            closeCommitteeItem(tag) {
                this.investedData.committeeList.splice(this.investedData.committeeList.indexOf(tag),1);
            },
//            //添加行业分类
//            changeIndustry(industryItem) {
//                let flag = false;
//                for (let i of this.industryList) {
//                    if (i.industry === industryItem) {
//                        flag = true;
//                        return;
//                    }
//                }
//                if (!flag) {
//                    this.investedData.industryList.push({industry: industryItem, fundId: '', id: ''})
//                }
//            }
        },
        components: {
            'csFundHeadEdit': require('./header/edit.vue'),
            'csFundBasicEdit': require('./basic/edit.vue'),
            'csFundAccountEdit': require('./account/edit.vue'),
            'csFundManageOrgan': require('./management-org/edit.vue'),//管理机构信息
            'csFundManage': require('./management/edit.vue'),//管理层
        },
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
